﻿using KinonekoSoftware.CodariaDB.Documents.Theories.Mineral;

namespace KinonekoSoftware.Studio.Pages.Theories.Minerals
{
    public sealed class NewMineralViewModel : AsyncObjectDialog<Mineral>
    {
        public static Task<Result<Mineral>> Create()
        {
            return Dialog<Mineral>(new NewMineralViewModel());
        }

        public static Task<Result<Mineral>> Edit(Mineral element)
        {
            var context = new DialogContext();
            context.Set(ContextBase.Key_Value, element);


            return Dialog<Mineral>(new NewMineralViewModel(), context);
        }

        
        private string            _name;
        private string            _color;
        private string            _intro;
        private Mineral           _target;
        private ImageEditFeedback _iconIEF;
        private object            _icon;

        public NewMineralViewModel()
        {
            ResetIconCommand   = CreateCommand(DoResetIconCommand);
            SelectIconCommand  = CreateCommand(DoSelectIconCommand);
        }
        
        
        protected override void CreateValidationRules(ICollection<DialogRule> rules)
        {
            rules.Add(DialogRule.Create<NewMineralViewModel>(string.Empty, x => !string.IsNullOrEmpty(x.Name)));
        }
        
        protected override async Task<Mineral> OnFinish(bool editing)
        {
            var iconFS = Platform.GetEngine<IconSystem>();
            
            if (editing)
            {
                _target.Name  = Name;
                _target.Color = Color;
                _target.Intro = Intro;
                
                //
                //
                if (_iconIEF is not null)
                {
                    _target.Icon = _iconIEF.ImageID;
                    await iconFS.WriteAsync(_iconIEF.ImageID, _iconIEF.ImageBuffer);
                }
            }
            else
            {
                _target = new Mineral
                {
                    Id    = ID.Get(),
                    Name  = Name,
                    Intro = Intro,
                    Color = Color,
                };
                
                //
                //
                if (_iconIEF is not null)
                {
                    _target.Icon = _iconIEF.ImageID;
                    await iconFS.WriteAsync(_iconIEF.ImageID, _iconIEF.ImageBuffer);
                }
            }

            return _target;
        }
        
        
        private async void DoSelectIconCommand()
        {
            var r = await Platform.Icon();

            if (!r.IsFinished)
            {
                if (!string.IsNullOrEmpty(r.Reason))
                {
                    await Danger(TitleSR.DangerOperation, r.Reason);
                }
                return;
            }

            _iconIEF    = r.Value;
            Icon = _iconIEF.Image;
        }
        
        

        private void DoResetIconCommand()
        {
            Icon     = null;
            _iconIEF = null;

            if (_target is not null)
            {
                _target.Icon = null;
            }
        }

        
        public object Icon
        {
            get => _icon;
            private set => SetValue(ref _icon, value);
        }
        
        
        public string Intro
        {
            get => _intro;
            set => SetValue(ref _intro, value);
        }
        
        public string Color
        {
            get => _color;
            set => SetValue(ref _color, value);
        }
        
        public string Name
        {
            get => _name;
            set => SetValue(ref _name, value);
        }
        
        public ICommandEX SelectIconCommand  { get; }
        public ICommandEX ResetIconCommand   { get; }
    }
}